import requests
from lxml import etree
from entity.News import News



def getClsNews():
    newsList = []
    webUrl = 'https://www.cls.cn'
    headers = {
        'User-Agent':
            'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/91.0.4472.124 Safari/537.36'
    }
    resp = requests.get(url=webUrl, headers=headers)
    resp.encoding = 'utf-8'
    resp = resp.text
    tree = etree.HTML(resp)


    a_list = tree.xpath(
        '/html/body/div[1]/div[1]/div[2]/div[2]/div[2]/div[3]//a[@target="_blank"]')
    for i in a_list:
        title = i.xpath('./text()')[0].replace("\n", "")
        if len(title) <= 1:  # 如果title为空则继续
            continue

        url = webUrl + i.xpath('./@href')[0]
        id = url.split('/')[-1].strip('.html')
        news = News("财联社",title,url,id)
        newsList.append(news.__dict__)
    return newsList

